import os
import django
import pandas as pd
import re
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'taobao.settings')
django.setup()
from app.models import Product

# 读取所有产品数据
products = Product.objects.all()

#名字评分
def ScoreName():
    #截取商品名字关键词，在用户看到名字时的购物评分
    keywords_scores = {
        '网红': -1,'实用': 1,'套装': 1.5,
        '优惠': 1,'活动': 1,'专用': 0.5,'好评': 0.5,
        '耐用': 1,'高级': 0.5,'学生': 0.5,'儿童': 1,
        '新鲜': 1,'鲜活': 1,'现取': 1,'甜': 0.5,
        '企业': 1,'企业级': -1.5,'商用': 0.5,'校园': 1,
        '稳定': 1.5,'易扩展': 1,'同款':-1.5,'工业级': 0.5,
        '回收': -999,
    }
    score = 1
    score_arr = []
    for s_names in products:
        s_name = s_names.name
        #print(s_name,11)
        for keyword, keyword_score in keywords_scores.items():
            #print(keyword,keyword_score)
            if keyword in s_name:
                score += keyword_score
                #print(s_name,keyword)
        #print(score)
        score_arr.append(score)
        score = 1
    return score_arr
#价格评分
def ScoreSold():
    score = 1   #商品评分
    all_sold = 0   #总价格
    num = 0   #商品计数
    score_arr = []

    #计算均价

    for solds in products:
        sold = solds.sold
        all_sold += float(sold)
        num += 1
    avg_sold = all_sold/num
    #print(avg_sold)
    '''
    计算价格得分，高于平均价格为扣分项，低于均价为加分，但过低的价格也会扣分
    '''
    for solds in products:
        sold = solds.sold
        #print(sold)

        j = sold/avg_sold
        if j == 0:
            score += 1.0
        elif j < 1:
            if j > 0.7:
                score += j
            elif j < 0.7:
                score -= avg_sold/sold
            elif j < 0.5:
                score -= 3   #低于均价一半，判定为恶意竞价
            else:
                score = -5

        elif j > 1:
            if j < 1.15:
                score += (j - 0.5)
            elif j < 1.3:
                score += (j - 1)
            elif j < 1.6:
                score -= j
            else:
                score = -5

        #print(score)
        score = round(score, 2)
        score_arr.append(score)
        score = 1
    #print(score_arr)
    return score_arr
#ScoreSold()

#通过宝贝描述/卖家服务/物流服务三项加权计算商家平均服务指数
def ScoreThree():

    score_arr = []
    weighting_1 = 0.419593  # 宝贝描述
    weighting_2 = 0.347504   # 卖家服务
    weighting_3 = 0.149722   # 物流服务
    for bb in products:
        bb_1 = bb.rating_1 * weighting_1
        bb_2 = bb.rating_2 * weighting_2
        bb_3 = bb.rating_3 * weighting_3
        score_bb = bb_1 + bb_2 + bb_3
        score_bb = round(score_bb, 2)
        score_arr.append(score_bb)
    #print(score_arr)
    return score_arr
#ScoreThree()

#通过销量来计算分数
def ScoreSales():

    #归一化处理，避免销量差距太大导致数据不和谐
    def normalize_number(number, min_value, max_value, new_min=1, new_max=8):
        # 将数字缩放到新的范围
        normalized_number = ((number - min_value) / (max_value - min_value)) * (new_max - new_min) + new_min
        return round(normalized_number, 6)

    original_value = []   #原始数值
    for sales_s in products:
        sales = sales_s.sales_volume
        pattern = r'(\d+)(\s?)(\d*万\+?)?'  # 匹配数字和万的格式
        match = re.search(pattern, sales)
        if match:
            num = match.group(1)  # 提取数字部分
            if match.group(3):  # 如果有万单位
                num = int(num) * 10000  # 将数字乘以10000
            original_value.append(int(num))
            #print(num)
        else:
            return None
    min_value = min(original_value)
    max_value = max(original_value)
    normalized_numbers = [normalize_number(number, min_value, max_value) for number in original_value]
    return normalized_numbers

def ScoreShopName():
    # 截取商品名字关键词，在用户看到名字时的购物评分
    keywords_scores = {
        '旗舰店': 1.5, '专卖店': 1.25, '天猫': 1.1,
        '专营店': 1.0,'工厂店': 0.75,'自营': 0.5,
    }
    score = 1
    score_arr = []
    for s_names in products:
        s_name = s_names.shop_name
        # print(s_name,11)
        for keyword, keyword_score in keywords_scores.items():
            # print(keyword,keyword_score)
            if keyword in s_name:
                score += keyword_score
                # print(s_name,keyword)
        # print(score)
        score_arr.append(score)
        score = 1
    #print(score_arr)
    return score_arr

#ScoreShopName()
def SingleDataAnalysis():

    def normalize_number(number, min_value, max_value, new_min=1, new_max=10):
        # 将数字缩放到新的范围
        normalized_number = ((number - min_value) / (max_value - min_value)) * (new_max - new_min) + new_min
        return round(normalized_number, 3)

    name_score = ScoreName()
    sold_score = ScoreSold()
    three_score = ScoreThree()
    sales_score = ScoreSales()
    show_name_score = ScoreShopName()
    # print(name_score)
    # print(sold_score)
    # print(three_score)
    # print(sales_score)
    # print(show_name_score)
    score_all = []
    for s1,s2,s3,s4,s5 in zip(name_score,sold_score,three_score,sales_score,show_name_score):
        score = s1*0.576 + s2*0.788 + s3*0.818 + s4*0.744 + s5*0.326
        #print(score)
        score_all.append(score)
    min_value = min(score_all)
    max_value = max(score_all)
    normalized_numbers = [normalize_number(number, min_value, max_value) for number in score_all]
    # for i in normalized_numbers:
    #     print(i)
    return name_score,sold_score,three_score,sales_score,show_name_score,normalized_numbers
